import { GETUSERDATA, LOGIN_USER } from './mutations-types'
import { loginAccount, getUserData, loginAccountMobile } from '@/service/get-data'
import { setCookie } from '@/config/mUntils'
export default {
  // 登录
  async GETUSER ({ commit }, form) {
    let res
    if (form.type === '1') {
      res = await loginAccount(form.username, form.password)
    } else if (form.type === '2') {
      res = await loginAccountMobile(form.mobile, form.code)
    }
    res = res.data
    if (res.resultCode && res.resultCode === 2000) {
      setCookie('Token', res.data.token) // token值
      setCookie('Exam', res.data.status) // 企业信息认证状态
      setCookie('needUpdateStatus', res.data.infoNeedUpdateStatus) // 完善企业
      setCookie('getTokenTime', Date.parse(new Date())) // token的时间戳
      setCookie('username', res.data.username) // 用户名
      setCookie('USERID', res.data.id) // 用户id
      commit(LOGIN_USER, res.data)
      // commit(FLASH_USER)
      // commit(FLASH_MOB_STATUS, {mobile: res.data.mobile})
      // commit(FLASH_EXAM, res.data.status)
      // commit(MESSAGENUM, res.data.messageNumber)
    }
    return res
  },
  async getUserData ({ commit }) {
    let res = await getUserData()
    setCookie('mobile', res.data.data.mobile) // 手机号
    commit(GETUSERDATA, res)
    return res
  }
}
